package com.konsung.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.konsung.bean.PatientBean;
import com.konsung.healthfile.R;
import com.konsung.kslibrary.base.BaseActivity;
import com.konsung.kslibrary.base.MvpView;
import com.konsung.presenter.AlterPatientPresenter;
import com.konsung.presenter.impl.AlterPatientPresenterImpl;
import com.konsung.ui.adapter.SpinnerAdapter;
import com.konsung.util.BroadcastUtils;
import com.konsung.util.ToastUtils;
import com.konsung.util.UiUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 修改居民档案信息的弹窗
 */
public class AlterPatientActivity extends BaseActivity<AlterPatientPresenterImpl> implements
        AlterPatientPresenter.View, MvpView {

    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_idcard)
    TextView tvIdcard;
    @BindView(R.id.et_contact)
    EditText etContact;
    @BindView(R.id.et_age)
    EditText etAge;
    @BindView(R.id.sp_sex)
    Spinner spSex;
    @BindView(R.id.sp_patient_type)
    Spinner spPatientType;
    @BindView(R.id.sp_blood_type)
    Spinner spBloodType;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.ll_add_layout)
    LinearLayout llAddLayout;
    @BindView(R.id.iv_head_icon)
    ImageView ivHeadIcon;

    PatientBean currentPatient;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_patient);
        ButterKnife.bind(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        initBindEvent();
    }

    @Override
    protected void onStart() {
        super.onStart();
        BroadcastUtils.sendDarkForStartManage();
        initViewData();
    }

    /**
     * 绑定点击事件
     */
    private void initBindEvent() {
        btnOk.setOnClickListener(this);
        ivClose.setOnClickListener(this);

    }

    /**
     * 初始化界面数据
     */
    private void initViewData() {
        currentPatient = presenter.getCurrentPatient();
        if (currentPatient == null) {
            ToastUtils.showShortToast(R.string.please_set_current_patient);
            finish();
            return;
        }
        String[] bloodType = UiUtils.getStringArr(R.array.blood_type);
        SpinnerAdapter bloodTypeAdapter = new SpinnerAdapter(this, bloodType);
        spBloodType.setPopupBackgroundDrawable(UiUtils.getDrawable(R.drawable.bg_drop_down_box));
        spBloodType.setAdapter(bloodTypeAdapter);

        String[] sexType = UiUtils.getStringArr(R.array.sex_type);
        SpinnerAdapter sexTypeAdapter = new SpinnerAdapter(this, sexType);
        spSex.setPopupBackgroundDrawable(UiUtils.getDrawable(R.drawable.bg_drop_down_box));
        spSex.setAdapter(sexTypeAdapter);

        String[] patientType = UiUtils.getStringArr(R.array.patient_type);
        SpinnerAdapter patientTypeAdapter = new SpinnerAdapter(this, patientType);
        spPatientType.setPopupBackgroundDrawable(UiUtils.getDrawable(R.drawable.bg_drop_down_box));
        spPatientType.setAdapter(patientTypeAdapter);
        spBloodType.setSelection(currentPatient.getBlood());
        spPatientType.setSelection(currentPatient.getPatientType());
        int sex = currentPatient.getSex();
        if (sex == 0) {
            sex = 2;
        } else if (sex == 1) {
            sex = 0;
        } else {
            sex = 1;
        }
        spSex.setSelection(sex);

        tvIdcard.setText(currentPatient.getIdcard());
        tvName.setText(currentPatient.getName());
        etAge.setText(String.valueOf(currentPatient.getAge()));
        etContact.setText(currentPatient.getContact());
        etAddress.setText(currentPatient.getAddress());
        UiUtils.setHeadPhoto(ivHeadIcon, currentPatient);
    }

    @Override
    public AlterPatientPresenterImpl initPresenter() {
        return new AlterPatientPresenterImpl();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_ok:
                String contact = etContact.getText().toString();
                int age = Integer.valueOf(etAge.getText().toString().trim().length() == 0 ?
                        "-1" : etAge.getText().toString());
                if (age > 300 || age < 0) {
                    ToastUtils.showShortToast(R.string.age_not_legal);
                    return;
                }
                int sex = spSex.getSelectedItemPosition();
                int patientType = spPatientType.getSelectedItemPosition();
                int bloodType = spBloodType.getSelectedItemPosition();
                String address = etAddress.getText().toString();
                currentPatient.setContact(contact);
                currentPatient.setAge(age);
                if (sex == 0) {
                    currentPatient.setSex(1);
                } else if (sex == 1) {
                    currentPatient.setSex(2);
                } else {
                    currentPatient.setSex(0);
                }
                currentPatient.setPatientType(patientType);
                currentPatient.setBlood(bloodType);
                currentPatient.setAddress(address);
                presenter.alterPatient(currentPatient);
                UiUtils.hideKeyboard(etAge);
                break;
            case R.id.iv_close:
                UiUtils.hideKeyboard(etAge);
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void alterResult(AlterState state) {
        switch (state) {
            case SUCCESS:
                ToastUtils.showShortToast(R.string.alter_success);
                Intent intent = new Intent(this, HealthFileActivity.class);
                startActivity(intent);
                finish();
                break;
            case FAILURE:
            default:
                ToastUtils.showShortToast(R.string.alter_failure);
                break;
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == 4) { //F1
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        BroadcastUtils.sendNormalForStartManage();
    }

    /**
     * 修改居民是否成功枚举
     */
    public enum AlterState {
        SUCCESS, //修改成功
        FAILURE, //修改失败
    }
}
